package net.caixiaomi.info.Lottery.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sobot.chat.utils.ScreenUtils;

import net.caixiaomi.info.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by suntian on 2018/8/19.
 */

public class BallOrderView extends LinearLayout{

    private List<BallOrderEntity> mList;
    private int mRow;   //列数
    private Context mContext;

    public BallOrderView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        setOrientation(VERTICAL);
        mContext=context;
//        List<BallOrderEntity>list=new ArrayList<>();
//        for(int i=0;i<7;i++){
//            BallOrderEntity entity=new BallOrderEntity();
//            entity.setNum("16");
//            entity.setColorType(0);
//            list.add(entity);
//        }
//        setEntity(list,7);
//        initView();
    }

    public void clearBalls(){
        if(mList!=null) {
            mList.clear();
        }
    }

    public void setEntity(List<BallOrderEntity> list,int row){
        mList=list;
        mRow=row;
        initView();
    }

    public void initView(){
        removeAllViews();
        int lines=mList.size()%mRow==0?mList.size()/mRow:mList.size()/mRow+1;
        for(int i=0;i<lines;i++){
            LinearLayout itemLin=new LinearLayout(mContext);
            itemLin.setOrientation(HORIZONTAL);
            LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            params.topMargin= ScreenUtils.dip2px(mContext,10);
            itemLin.setLayoutParams(params);

            for(int j=0;j<mRow;j++){
                int index=i*mRow+j;
                if(index<mList.size()){
                    BallOrderEntity entity=mList.get(index);
                    View itemBall=View.inflate(mContext, R.layout.order_ball_view,null);
                    RelativeLayout lGroup=itemBall.findViewById(R.id.ball_view_group);
                    TextView lTv=itemBall.findViewById(R.id.ball_view_tv);
                    View divider=itemBall.findViewById(R.id.ball_view_group_divider);
                    lGroup.setVisibility(VISIBLE);
                    divider.setVisibility(GONE);
                    lTv.setText(entity.getNum());
                    switch (entity.getColorType()){
                        case 0: //实心红球
                            lGroup.setBackgroundResource(R.drawable.red_ball);
                            lTv.setTextColor(getResources().getColor(R.color.white));
                            break;
                        case 1: //空心红球
                            lGroup.setBackgroundResource(R.drawable.red_circle);
                            lTv.setTextColor(getResources().getColor(R.color.red));
                            break;
                        case 2: //实心蓝球
                            lGroup.setBackgroundResource(R.drawable.blue_ball);
                            lTv.setTextColor(getResources().getColor(R.color.white));
                            break;
                        case 3: //空心蓝球
                            lGroup.setBackgroundResource(R.drawable.blue_circle);
                            lTv.setTextColor(getResources().getColor(R.color.blue));
                            break;
                        case 4: //空格
                            lGroup.setBackgroundResource(R.color.white);
                            lTv.setTextColor(getResources().getColor(R.color.primary_text));
                            break;
                        case 5: //破折号
                            lGroup.setVisibility(GONE);
                            divider.setVisibility(VISIBLE);
                            lGroup.setBackgroundResource(R.color.white);
                            lTv.setTextColor(getResources().getColor(R.color.primary_text));
                            break;
                    }
                    if(entity.getDivider()==1){
                        itemBall.findViewById(R.id.order_view_divider).setVisibility(VISIBLE);
                    }else{
                        itemBall.findViewById(R.id.order_view_divider).setVisibility(GONE);
                    }
                    itemLin.addView(itemBall);
                }
            }
            addView(itemLin);
        }
    }
}
